import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.Timer;




public class Gui extends Panel{
	private final int FPS = 30;
	 BufferedImage map;	
	 BufferedImage panel = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
	 BufferedImage black;
	 BufferedImage white;
	 BufferedImage selection;
	 boolean initiated = false;
	 Timer timer;
	 Game game;
	 public Gui(Game game){
		 this.game = game;
			//FPS is frames per second to draw to screen;
		 loadImages();	
		 
		 timer = new Timer(1000/FPS, new ActionListener(){

			
				
				//timer to set when to draw the screen and update values
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					draw();
					timer.restart();
					
				}

				});
		 
		 JFrame jf = new JFrame("Simulation");
		  this.setFocusable(true);
		  this.requestFocusInWindow();
		   jf.setFocusable(true);
		   jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	  
		   jf.getContentPane().add(this);
		   jf.getContentPane().setPreferredSize(new Dimension(600,600));	 
		   jf.validate();
		   jf.pack();
		   jf.setVisible(true);
		 initiated = true;
		 timer.start();
		 //draw();
			this.addMouseListener(new MouseListener(){

				@Override
				public void mouseClicked(MouseEvent click) {
					
					int x = (click.getX()/75)%8;
					int y = (click.getY()/75)%8;
					
					if( click.getButton() == MouseEvent.BUTTON1)
					makeTurn(x,y);
					/*if( click.getButton() == MouseEvent.BUTTON2)
						makeTurn();*/
						//set(x,y,0);
					/*if( click.getButton() == MouseEvent.BUTTON3)
						set(x,y,2);*/
					
					
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub
					
				}});
	 }
	 
	 public void loadImages(){
		    try {
		    	//getClass().getResource("/image.jpg")
				map = ImageIO.read(getClass().getResource("/board.png"));
				black = ImageIO.read(getClass().getResource("/black.png"));
				white = ImageIO.read(getClass().getResource("/white.png"));
				selection = ImageIO.read(getClass().getResource("/selection.png"));
			} catch (IOException e) {
				System.out.println("files not found");
				
			}
	 }
	 
	 private void set(int x, int y, int color){
		 game.getBoard().set(x,y,color);
	 }
	 
	 private void draw() {
		 if(!initiated)
			 return;
			// TODO Auto-generated method stub
			Graphics2D g = (Graphics2D) panel.getGraphics();
			g.drawImage(map,0,0,600,600,null);
			int[][] board = game.getBoard().getData();
			
			
			drawSelections(g);
			drawPieces(g, board);
			g.setColor(Color.RED);
			g.drawString("white:" + game.getWhitePieces(), 0, 20);
			g.drawString("black:" + game.getBlackPieces(), 0, 40);
			
			g.dispose();
			repaint();
		}

	private void drawSelections(Graphics2D g) {
		for (Coordinate co : game.getBoard().getPossibleMoves((game.getTurn())?1:2)) {
			g.drawImage(selection,co.x*75,co.y*75,null);
		}
	}

	private void drawPieces(Graphics2D g, int[][] board) {
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board.length; j++) {
				if (board[j][i] == 1){
					g.drawImage(white,j*75,i*75,null);
				}else if (board [j][i] == 2){
					g.drawImage(black,j*75,i*75,null);
				}
			}
		}
	}
	 
	 @Override
		public void update(Graphics g){
		       paint(g);
		}
		
		@Override
		public void paint(Graphics g) {
			if(panel == null){
				panel = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
				draw();
				}
			g.drawImage(panel,0,0,getWidth(),getHeight(),null);
			
		}
	 
	 
	 public static void main(String args[]){
		Game game = new Game();
		 Gui g = new Gui(game);
	 }
	 
	 
	 public void makeTurn(int x, int y){
		 if (game.getPossibleMoves().contains(new Coordinate(x,y)));
		 {
			System.out.println("contains: " + new Coordinate(x,y).toString());
			 for (Coordinate co :game.getPossibleMoves()) {
				System.out.println(co.toString());
			}
			 
		 game.playerTurn(x,y);
		 }
	 }
	 
	 
	 
}
